package com.example.demo1;

import static androidx.activity.result.ActivityResultCallerKt.registerForActivityResult;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Menu;

import com.example.demo1.entity.JumpCode;
import com.example.demo1.entity.Student;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.navigation.NavController;
import androidx.navigation.NavDeepLink;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;

import com.example.demo1.databinding.ActivityMainBinding;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity->";

    /*private ActivityResultLauncher<Intent> intentActivityResultLauncherForRename = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
        @Override
        public void onActivityResult(ActivityResult result) {
        }
    });*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //修改切换后台之后在进入回首页问题
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }

        Log.d(TAG, "onCreate>>>taskId = " + getTaskId());
        findViewById(R.id.btn_intent_1).setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, SettingActivity.class);
            startActivity(intent);
        });

        findViewById(R.id.btn_intent_2).setOnClickListener(v -> {

//            Intent intent = new Intent("android.intent.action.VIEWPDF");
//            intent.setType("application/pdf");
//            startActivity(intent);

            /*Intent intent = new Intent("android.intent.action.VIEWTEXT");
            intent.addCategory("android.intent.category.MY_DEFAULT");
            intent.setType("application/text");
            startActivity(intent);*/

            Intent intent = new Intent("android.intent.action.VIEWTEXT");
            intent.setType("application/text");
            startActivity(intent);

        });
        findViewById(R.id.btn_intent_view).setOnClickListener(v -> {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            startActivity(intent);
        });
        findViewById(R.id.btn_intent_call).setOnClickListener(v -> {
            //PS:注意授权
            if (checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 1);
                return;
            }
            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:15100000000"));
            startActivity(intent);
        });
        findViewById(R.id.btn_intent_sms).setOnClickListener(v -> {
            //PS:注意授权
            if (checkSelfPermission(Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.SEND_SMS}, 1);
                return;
            }
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.putExtra("sms_body", "Hello World!");
            intent.setType("vnd.android-dir/mms-sms");
            startActivity(intent);
        });
        findViewById(R.id.btn_show_dialog).setOnClickListener(v -> {
            AlertDialog.Builder normalDialog =
                    new AlertDialog.Builder(MainActivity.this);
            normalDialog.setIcon(R.drawable.ic_menu_camera);
            normalDialog.setTitle("我是一个普通Dialog").setMessage("你要点击哪一个按钮呢?");
            normalDialog.setPositiveButton("按钮1",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // ...To-do
                        }
                    });
            normalDialog.setNeutralButton("按钮2",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // ...To-do
                        }

                    });
            normalDialog.show();
        });
        findViewById(R.id.btn_jump_2_second).setOnClickListener(v -> {
            startActivity(new Intent(MainActivity.this, SecondActivity.class));
        });
        findViewById(R.id.btn_jump_2_Main).setOnClickListener(v -> {
            startActivity(new Intent(MainActivity.this, MainActivity.class));
        });
        findViewById(R.id.btn_data_transfer).setOnClickListener(v -> {

            Intent intent = new Intent(MainActivity.this, SettingActivity.class);
            intent.putExtra(SettingActivity.PARAMS_STUDENT, new Student("张三", 18));
            startActivity(intent);

            //TODO 通过bundle传递演示

        });
        findViewById(R.id.btn_layout).setOnClickListener(v -> {

            Intent intent = new Intent(MainActivity.this, SettingActivity.class);
            intent.putExtra(SettingActivity.PARAMS_STUDENT, new Student("张三", 18));
            startActivity(intent);

        });

        findViewById(R.id.btn_screen_orientation_test).setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, ScreenOrientationActivity.class);
            intent.putExtra(ScreenOrientationActivity.PARAMS_STU, new Student("张三", 18));
            startActivity(intent);
        });
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.d(TAG, "onNewIntent");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        Log.d(TAG, "onWindowFocusChanged>>>>hasFocus = " + hasFocus);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case JumpCode.REQ_SETTING:
                if (resultCode == JumpCode.RES_SETTING) {
                    Student student = (Student) data.getSerializableExtra(SettingActivity.PARAMS_STUDENT_RETURN);
                    Log.d(TAG, "onActivityResult>>>student = " + student);
                }
                break;
        }
    }
}